package me.itsoo.artemis.framework.core.converter.number;

import java.math.BigDecimal;

/**
 * BigDecimalTypeConvertor
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/18
 */
public class BigDecimalTypeConvertor implements NumberTypeConvertor {

    @Override
    public Object convertIfNecessary(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof BigDecimal) {
            return value;
        }

        if (value instanceof Number) {
            return BigDecimal.valueOf(((Number) value).longValue());
        }

        if (value instanceof String) {
            return new BigDecimal((String) value);
        }

        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return null;
        }
    }
}
